extends CenterContainer



const SpriteAttachItem = preload("res://tool/page/poke/modal/SpriteAttachItem.tscn")

onready var item_box = $PanelContainer/VBoxContainer/itemBox

var edit_data = null

var poke_data



func set_data(poke_data):
	self.poke_data = poke_data
	edit_data = DataSource.get_data_by_one(DataSource.poke_item, poke_data["id"])
	
	if edit_data:
		var item_list = edit_data.get("itemList", [])
		
		for item in item_list:
			var ui = SpriteAttachItem.instance()
			item_box.add_child(ui)
			
			ui.set_data(item)
	
	$PanelContainer/VBoxContainer/modalTitle.text = "%s 携带道具" % poke_data["name"]
		

func _on_cancel_button_up():
	MainTool.getUILayer().pop_ui()


func _on_ok_button_up():
	var item_list = []
	
	for child in item_box.get_children():
		item_list.append(child.get_data())
	
	
	if edit_data:
		edit_data["itemList"] = item_list
			
		DataSource.update_data(DataSource.poke_item, edit_data)
	else:
		var data = {
			"id": poke_data["id"],
			"itemList": item_list,
		}
		
		DataSource.add_data(DataSource.poke_item, data)
	
	MainTool.toast_info("更新成功!")
	
	MainTool.getUILayer().pop_ui()


func _on_addItem_button_up():
	if item_box.get_child_count() >= 5:
		MainTool.toast_info("宝可梦携带道具不能超过5个")
		return
		
	var ui = SpriteAttachItem.instance()
	
	item_box.add_child(ui)
